package com.time.workbench.efficient.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.time.workbench.auth.entity.bo.Roles;
import com.time.workbench.core.util.AssertUtil;
import com.time.workbench.core.util.AuthUtil;
import com.time.workbench.efficient.entity.bo.Application;
import com.time.workbench.efficient.entity.bo.ApplicationServer;
import com.time.workbench.efficient.entity.bo.EnvironmentServer;
import com.time.workbench.efficient.entity.dto.ApplicationPageReq;
import com.time.workbench.efficient.entity.dto.SetApplicationServerReq;
import com.time.workbench.efficient.entity.vo.GitCommitVO;
import com.time.workbench.efficient.service.ApplicationServerService;
import com.time.workbench.efficient.service.ApplicationService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 应用管理
 *
 * @description: 应用管理
 * @menu 应用管理
 */
@RestController
@RequestMapping("/api/efficient/application")
@Slf4j
@RequiredArgsConstructor
public class ApplicationController {

    public final ApplicationService applicationService;

    private final ApplicationServerService applicationServerService;

    @GetMapping("/branch/{id}")
    public List<String> branch(@PathVariable String id) {
        return applicationService.listBranch(id);
    }

    @GetMapping("/commit/{id}")
    public List<GitCommitVO> branch(@PathVariable String id, String branch) {
        return applicationService.listCommit(id, branch);
    }

    @GetMapping("/list")
    public List<Application> list() {
        return applicationService.lambdaQuery().orderByDesc(Application::getId).list();
    }

    /**
     * 设置服务器
     *
     * @param setApplicationServerReq
     * @param applicationId
     */
    @PostMapping("/setServer/{id}")
    public void setServer(@Validated @RequestBody SetApplicationServerReq setApplicationServerReq, @PathVariable("id") Integer applicationId) {
        applicationServerService.setServer(setApplicationServerReq, applicationId);
    }

    /**
     * 获取环境和应用所有服务器
     *
     * @param environmentId
     * @return
     */
    @GetMapping("/getApplicationServer/{applicationId}/{environmentId}")
    public List<ApplicationServer> getApplicationServer(@PathVariable("applicationId") Integer applicationId, @PathVariable("environmentId") Integer environmentId) {
        return applicationServerService.getApplicationServer(applicationId, environmentId);
    }

    /**
     * 分页查询
     *
     * @param applicationPageReq
     * @return
     * @status done
     */
    @GetMapping("/page")
    public Page<Application> page(@Validated ApplicationPageReq applicationPageReq) {
        return applicationService.page(applicationPageReq);
    }

    /**
     * 保存应用
     *
     * @param application
     * @status done
     */
    @PostMapping("")
    public void save(@RequestBody @Validated Application application) {
        application.setTenantId(AuthUtil.getCurrTenantId());
        applicationService.save(application);
    }

    /**
     * 修改应用
     *
     * @param application
     * @param id
     * @status done
     */
    @PutMapping("/{id}")
    public void update(@RequestBody @Validated Application application, @PathVariable("id") Integer id) {
        application.setId(id);
        applicationService.updateById(application);
    }

    /**
     * 删除应用
     *
     * @status done
     */
    @DeleteMapping("/{id}")
    public void remove(@PathVariable("id") Integer id) {
        applicationService.removeById(id);
    }

}
